package com.linkai.onlinetool.controller;

import com.alibaba.fastjson.JSON;
import com.linkai.onlinetool.entity.Comment;
import com.linkai.onlinetool.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/** 
* @Description: 处理评论相关
* @Param:  
* @return:  
* @Author: 林凯
* @Date: 2020/10/11 
*/ 
@RestController
public class CommentController {

    @Autowired
    CommentService commentService;

    // 记得处理跨域问题
    /** 
    * @Description:  发表评论
    * @Param: [map] 
    * @return: java.lang.String 
    * @Author: 林凯
    * @Date: 2020/10/11 
    */ 
    @PostMapping("/comment")
//    @CrossOrigin(value = "http://localhost:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String comment(@RequestBody HashMap<String, String> map) {
        System.out.println(map.get("nickName") + map.get("commentText"));

        Comment comment = new Comment(null, 0, map.get("nickName"), map.get("commentText"), new Date());
        Comment result = commentService.insert(comment);

        if (result != null) {
            // 插入数据成功
            return "200";
        } else {
            // 插入数据失败
            return "400";
        }

    }


    /**
    * @Description: 发表回复
    * @Param: []
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/11
    */
    @PostMapping("/comment/reply")
//    @CrossOrigin(value = "http://localhost:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String commentReply(@RequestBody HashMap<String, String> map) {
        System.out.println(map);
        Comment comment = new Comment(null, Integer.parseInt(map.get("fId")), map.get("replyNickName"), map.get("replyCommentText"), new Date());
        Comment insert = commentService.insert(comment);
        if (insert != null) {
            return "200";
        } else {
            return "400";
        }
    }


    /**
    * @Description: 获取评论数据，带分页
    * @Param: []
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/11
    */
    @GetMapping("/comment/getDataPage")
//    @CrossOrigin(value = "http://101.132.147.111:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String getDataPage() {
        List<Comment> comments = commentService.queryAllByLimit(0, 3);

        return "";
    }

    /**
    * @Description: 获取全部评论数据
    * @Param: []
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/11
    */
    @GetMapping("/comment/getAllData")
//    @CrossOrigin(value = "http://101.132.147.111:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String getAllData() {

        List<Comment> comments = commentService.queryAllData();
//        System.out.println(comments);
        String result = JSON.toJSONString(comments);
//        System.out.println(result);

        return result;
    }
}
